package com.example.takeaway.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.example.takeaway.entity.Merchant;
import com.example.takeaway.serivce.IMerchantService;
import com.example.takeaway.utils.PageResult;
import com.example.takeaway.utils.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * merchantController
 * 
 * @author ruoyi
 * @date 2025-05-15
 */
@RestController
@RequestMapping("/system/merchant")
public class MerchantController
{
    @Autowired
    private IMerchantService merchantService;

    /**
     * 查询merchant列表
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:list')")
    @GetMapping("/list")
    public ServerResponse list(Merchant merchant, @RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize)
    {
//        startPage();
        PageResult pageResult = merchantService.selectMerchantList(merchant,pageNum,pageSize);
        return ServerResponse.createBysuccess(200,pageResult,"查询成功");
    }

    /**
     * 导出merchant列表
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:export')")
//    @Log(title = "merchant", businessType = BusinessType.EXPORT)
//    @PostMapping("/export")
//    public void export(HttpServletResponse response, Merchant merchant)
//    {
//        List<Merchant> list = merchantService.selectMerchantList(merchant);
//        ExcelUtil<Merchant> util = new ExcelUtil<Merchant>(Merchant.class);
//        util.exportExcel(response, list, "merchant数据");
//    }

    /**
     * 获取merchant详细信息
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:query')")
    @GetMapping(value = "/{merchantid}")
    public ServerResponse getInfo(@PathVariable("merchantid") Long merchantid)
    {
      Merchant merchant =  merchantService.selectMerchantByMerchantid(merchantid);
       return  ServerResponse.createBysuccess(200,merchant,"查询成功");
    }

    @GetMapping("/search")
    public ServerResponse search(@RequestParam String search,@RequestParam(defaultValue = "1") int pageNum, @RequestParam(defaultValue = "10")int pageSize){


           PageResult pageResult = merchantService.search(search,pageNum,pageSize);
        return ServerResponse.createBysuccess(200,pageResult,"查询成功");
}
    /**
     * 新增merchant
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:add')")
//    @Log(title = "merchant", businessType = BusinessType.INSERT)
    @PostMapping
    public ServerResponse add(@RequestBody Merchant merchant)
    {
        int i = merchantService.insertMerchant(merchant);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 修改merchant
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:edit')")
//    @Log(title = "merchant", businessType = BusinessType.UPDATE)
    @PutMapping
    public ServerResponse edit(@RequestBody Merchant merchant)
    {
        int i = merchantService.updateMerchant(merchant);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }

    /**
     * 删除merchant
     */
//    @PreAuthorize("@ss.hasPermi('system:merchant:remove')")
//    @Log(title = "merchant", businessType = BusinessType.DELETE)
	@DeleteMapping("/{merchantids}")
    public ServerResponse remove(@PathVariable Long[] merchantids)
    {
        int i = merchantService.deleteMerchantByMerchantids(merchantids);
        if (i>0){
            return  ServerResponse.createBysuccess(200,"","新增成功");
        }else{
            return  ServerResponse.createByfail(400,"新增失败");
        }
    }
}
